package pw.archipelag.loaderGame.panel {
	import base.dspObj.DoOperations;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinPanelMini;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinBg;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinBtnPanel;
	import flash.geom.Rectangle;

	import flash.events.Event;
	
	public class PanelMini extends PanelAbstractBg {
		
		private static const MARGIN_X_STAGE_DEFAULT: Number = 20;
		private static const MARGIN_Y_STAGE_DEFAULT: Number = 10;
		
		public var btnHelp: BtnPanel;
		public var btnScores: BtnPanel;
		private var skinPanelMini: SkinPanelMini;
		
		public function PanelMini(): void {
			super(0);
			this.createBtns();
			this.alpha = 0;
			DoOperations.hideShow(this, 1);
		}
		
		private function createBtns(): void {
			this.btnHelp = new BtnPanel("Pomoc");
			this.addChild(this.btnHelp);
			this.btnScores = new BtnPanel("Najlepsi");
			this.addChild(this.btnScores);
		}
		
		private function removeBtns(): void {
			this.btnHelp.destroy();
			this.removeChild(this.btnHelp);
			this.btnHelp = null;
			this.btnScores.destroy();
			this.removeChild(this.btnScores);
			this.btnScores = null;
		}
		
		public function draw(skinPanelMini: SkinPanelMini = null, skinBg: SkinBg = null, skinBtnPanel: SkinBtnPanel = null): void {
			super.drawToBg(skinBg || new SkinBg());
						
			skinBtnPanel = skinBtnPanel || new SkinBtnPanel();
			this.btnHelp.draw(skinBtnPanel);
			this.btnScores.draw(skinBtnPanel);
			
			skinPanelMini = skinPanelMini || new SkinPanelMini();
			skinPanelMini.dimension = skinPanelMini.dimension || new Rectangle();
			skinPanelMini.dimension.x = skinPanelMini.dimension.x || PanelMini.MARGIN_X_STAGE_DEFAULT; 
			skinPanelMini.dimension.y = skinPanelMini.dimension.y || PanelMini.MARGIN_Y_STAGE_DEFAULT;
			if (skinPanelMini.isBtnsArrangedHorizontalVertical == 0) {
				this.btnScores.x = this.btnHelp.x + this.btnHelp.width + PanelAbstractBg.MARGIN_X_ELEMENTS;
				skinPanelMini.dimension.width = skinPanelMini.dimension.width || this.btnScores.x + this.btnScores.width + 2 * PanelAbstractBg.MARGIN_X_ELEMENTS, 
				skinPanelMini.dimension.height = skinPanelMini.dimension.height || this.btnHelp.height + 2 * PanelAbstractBg.MARGIN_Y_ELEMENTS;
			} else if (skinPanelMini.isBtnsArrangedHorizontalVertical == 1) {
				this.btnScores.y = this.btnHelp.y + this.btnHelp.height + PanelAbstractBg.MARGIN_Y_ELEMENTS;
				skinPanelMini.dimension.width = skinPanelMini.dimension.width || this.btnHelp.width + 2 * PanelAbstractBg.MARGIN_X_ELEMENTS;
				skinPanelMini.dimension.height = skinPanelMini.dimension.height || this.btnScores.y + this.btnScores.height + 2 * PanelAbstractBg.MARGIN_Y_ELEMENTS;
			}
			this.skinPanelMini = skinPanelMini;
			this.setDimension();
			if (this.stage.hasEventListener(Event.RESIZE)) this.stage.removeEventListener(Event.RESIZE, this.onStageResize);
			this.stage.addEventListener(Event.RESIZE, this.onStageResize);
			this.stage.dispatchEvent(new Event(Event.RESIZE));
		}
		
		private function onStageResize(e: Event):void {
			this.setDimension();
		}
		
		private function setDimension(): void {
			this.width = this.skinPanelMini.dimension.width;
			this.height = this.skinPanelMini.dimension.height;
		}
		
		public function hideShow(isHideShow: uint): void {
			if (isHideShow == 0) {
				this.btnHelp.removeMouseEvents();
				this.btnScores.removeMouseEvents();
			} else if (isHideShow == 1) {
				this.btnHelp.addMouseEvents();
				this.btnScores.addMouseEvents();
			}
			DoOperations.hideShow(this, isHideShow);
			this.parent.setChildIndex(this, [0, this.parent.numChildren - 1][isHideShow]);
		}
		
		override public function get width(): Number {
			return super.width;
		}
		
		override public function get height(): Number {
			return super.height;
		}
		
		override public function set width(value: Number): void {
			super.width = value;
			var marginXBtn: Number;
			if (this.skinPanelMini.isBtnsArrangedHorizontalVertical == 0) {
				marginXBtn = (value - this.btnHelp.width - this.btnScores.width) / 3;
				this.btnHelp.x = marginXBtn;
				this.btnScores.x = marginXBtn + this.btnHelp.width + marginXBtn;
			} else if (this.skinPanelMini.isBtnsArrangedHorizontalVertical == 1) {
				marginXBtn = (value - this.btnHelp.width) / 2;
				this.btnHelp.x = this.btnScores.x = marginXBtn;
			}
			this.x = Math.round([this.skinPanelMini.dimension.x,  this.stage.stageWidth - this.skinPanelMini.dimension.x - this.width][this.skinPanelMini.isSetPositionFromTopLeftRightBottomLeftRight % 2]);
		}
		
		override public function set height(value: Number): void {
			super.height = value;
			var marginYBtn: Number;
			if (this.skinPanelMini.isBtnsArrangedHorizontalVertical == 0) {
				marginYBtn = (value - this.btnHelp.height) / 2;
				this.btnHelp.y = this.btnScores.y = marginYBtn;
			} else if (this.skinPanelMini.isBtnsArrangedHorizontalVertical == 1) {
				marginYBtn = (value - this.btnHelp.height - this.btnScores.height) / 3;
				this.btnHelp.y = marginYBtn;
				this.btnScores.y = marginYBtn + this.btnHelp.height + marginYBtn;
			}
			this.y = Math.round([this.skinPanelMini.dimension.y,  this.stage.stageHeight - this.skinPanelMini.dimension.y - this.height][Math.floor(this.skinPanelMini.isSetPositionFromTopLeftRightBottomLeftRight / 2)]);
		}
		
		override public function destroy(): void {
			this.removeBtns();
			super.destroy();
		}
		
	}

}